package com.itheima.controller;

import com.itheima.anno.Log;
import com.itheima.pojo.Emp;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Result;
//import lombok.extern.java.Log;
import com.itheima.service.EmpService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * 员工管理Controller
 */
@Slf4j
@RestController
public class EmpController {

    @Autowired
    private EmpService empService;

    @GetMapping("/emps")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String name, Short gender,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate begin,
                       @DateTimeFormat(pattern = "yyyy-MM-dd") LocalDate end) {
        log.info("分页查询，参数: {},{},{},{},{},{}",page,pageSize,name,gender,begin,end);
        //调用service查询记录数
        PageBean pageBean = empService.page(page,pageSize,name,gender,begin,end);
        return Result.success(pageBean);
    }

    @Log
    @DeleteMapping("/emps/{ids}")
    public Result delete(@PathVariable List<Integer> ids){
        log.info("根据id删除员工:{}",ids);
        //调用service删除员工
        empService.delete(ids);

        return Result.success();
    }

    @Log
    @PostMapping("/emps")
    public Result add(@RequestBody Emp emp){
        log.info("新增员工:{}",emp);
        //调用service新增员工
        empService.add(emp);

        return Result.success();
    }

    @GetMapping("/emps/{id}")
    public Result query(@PathVariable Integer id){
        log.info("根据id查询员工:{}",id);
        //调用service查询员工
        Emp emp = empService.query(id);

        return Result.success(emp);
    }

    @Log
    @PutMapping("/emps")
    public Result alter(@RequestBody Emp emp){
        log.info("修改员工数据:{}",emp);
        //调用service修改员工数据
        empService.alter(emp);
        return Result.success();
    }


}
